package servlet;

import user.Order;
import user.ShopCar;
import util.DBUtilDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class SelectOrderCarServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        List<Order> orders = null;
        float order = 0;
        DBUtilDao dbUtilDao = new DBUtilDao();

        try {
            orders = dbUtilDao.getOrder(username, 0);//根据手机号，查询数据库中的未支付订单信息
            for (Order order1 : orders) {
                order = order + order1.getTotalPrice();
                session.setAttribute("order", order);//计算未支付订单总价
            }
            session.setAttribute("orderCar", orders);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            orders = dbUtilDao.getOrder(username, 1); //查询数据库中已经支付的订单信息
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        for (Order order1 : orders) {
            order = order + order1.getTotalPrice();
            session.setAttribute("ordered", order);//计算已经支付订单总价

        }
        session.setAttribute("orderCared", orders);
        try {
            dbUtilDao.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
